﻿//生效1为生效0为取消
function btnDetermine_click(obj) {
    if (document.getElementById('hiduserid') == null) {
        return false
    }
    var userid = document.getElementById('hiduserid').value;
    
    var deptid = document.getElementById('hiddeptid').value;
    var drop = document.getElementById('dropPK');
    var index = drop.selectedIndex; //序号，取当前选中选项的序号
    var val = drop.options[index].value;
    if (val == '0') {
        alert('请选择套餐!');
        return false;
    }
    $.ajax({
        type: "POST",
        url: "AjaxServer.aspx",
        dataType: 'json',
        data: "alt=Determine&userid=" + userid + "&deptid=" + deptid + "&type=" + obj + "",
        processData: false,
        timeout: '10000',
        success: function(msg) {

            if (msg.msg == '1') {
                alert('默认套餐已生效');
                document.getElementById('btnDetermine').disabled = true;
            }
            else {
                alert('默认套餐已取消');
                document.getElementById('btnDetermine').value = '生效';
                document.getElementById('btnDetermine').disabled = false;
            }
        },
        error: function() {
            alert('系统繁忙!');
        }

    });
}







function CKdelete() {
    var drop = document.getElementById('dropPK');
    var index = drop.selectedIndex; //序号，取当前选中选项的序号

    var val = drop.options[index].value;
    if (confirm('确定要删除' + val + '套餐?')) {
        if (val == '0') {
            return false;
        }
        else {

            return true;
        }
        return true;
    }
    else {
        return false;
    }
}

function YZname() {

    var dropPK = document.getElementById('dropPK');

    if (dropPK != null) {
        var index = dropPK.selectedIndex; //序号，取当前选中选项的序号
        var valuu = dropPK.options[index].value;
        //                    if (document.getElementById('txttext').value == "") {
        //                        alert('套餐名称不能空！');
        //                        return false;
        //                    }
        //                    if (valuu == document.getElementById('txttext').value) {
        //                        alert('套餐名称不能重复！');
        //                        return false;
        //                    }

        var val = dropPK.options[index].text;
        var ta = document.getElementById('table2');

        if (val != '请选择') {
            var he1 = parseInt(document.getElementById('txtallnum2').value);
            var he2 = parseInt(document.getElementById('txtTotal2').value);

            if ((he1 - he2) < 0) {
                alert('不能低于最低分数！');
                return false;
            }
            var j = 0;
            var k = 0;
            for (var i = 1; i < ta.rows.length - 2; i++) {
                var cell = ta.rows[i].cells[5].childNodes[0].value;

                var chk = ta.rows[i].cells[1].childNodes[0].checked;
                if (cell != "0" && chk == true) {
                    j++;
                }
                if (chk == true) {
                    k++;
                }
            }
            if (k == 0 || j == 0) {
                alert('请正确选择或填写数量！')
                return false;
            }
            if (j == k) {
                return true;
            }
            else {
                alert('请正确选择或填写数量！')
                return false;
            }

        }
    }
    else {
        if (document.getElementById('txtName').value == "") {
            alert('套餐名称不能空！');
            return false;
        }
        var he1 = parseInt(document.getElementById('txtallnum1').value);
        var he2 = parseInt(document.getElementById('txtTotal1').value);

        if ((he1 - he2) < 0) {
            alert('不能低于最低分数！');
            return false;
        }

        ta = document.getElementById('table1');
        var j = 0;
        var k = 0;
        for (var i = 1; i < ta.rows.length - 2; i++) {
            var cell = ta.rows[i].cells[5].childNodes[0].value;

            var chk = ta.rows[i].cells[1].childNodes[0].checked;

            if (cell != "0" && chk == true && cell != "") {
                j++;
            }
            if (chk == true) {
                k++;
            }
        }
        if (k == 0 || j == 0) {
            alert('请正确选择或填写数量！')
            return false;
        }
        if (j == k) {
            return true;
        }
        else {
            alert('请正确选择或填写数量！')
            return false;
        }


    }






    return true;
}





function CheckAll(o) {
    $("#table1 tbody input[@type=checkbox]").attr("checked", o.checked);
}


window.onload = function() {

    var dropPK = document.getElementById('dropPK');
    if (dropPK != null) {
        var index = dropPK.selectedIndex; //序号，取当前选中选项的序号
        var val = dropPK.options[index].text;
        //var tablename = document.getElementById('table2');
        if (val != '请选择') {
            var tablename = document.getElementById('table2');
            tablename.rows[1].cells[0].rowSpan = '6';
            for (var i = 2; i < 7; i++) {
                tablename.rows[i].cells[0].style.display = 'none';
            }
            tablename.rows[7].cells[0].rowSpan = '7';
            for (var i = 8; i < 14; i++) {
                tablename.rows[i].cells[0].style.display = 'none';
            }
        }

        if (val != "请选择") {
            var tabnumn = 0;
            var tablename = 'table2';
            for (var i = 1; i < document.getElementById(tablename).rows.length - 2; i++) {
                if (document.getElementById(tablename).rows[i].cells[1].childNodes[0].checked || document.getElementById(tablename).rows[i].cells[1].childNodes[0].disabled) {
                    tabnumn += parseInt(document.getElementById(tablename).rows[i].cells[6].childNodes[0].value == '' ? '0' : parseInt(document.getElementById(tablename).rows[i].cells[6].childNodes[0].value));
                }
            }

            document.getElementById('txtallnum2').value = tabnumn;

        }
    }
    else {
        var tablename = document.getElementById('table1');
        tablename.rows[1].cells[0].rowSpan = '6';
        for (var i = 2; i < 7; i++) {
            tablename.rows[i].cells[0].style.display = 'none';
        }
        tablename.rows[7].cells[0].rowSpan = '7';
        for (var i = 8; i < 14; i++) {
            tablename.rows[i].cells[0].style.display = 'none';
        }
    }
}


function Knum(obj, tablename, n) {
    var rowIndex = obj.parentNode.parentNode.rowIndex;
    var tbcell3 = document.getElementById(tablename).rows[rowIndex].cells[3].innerHTML; //分值
    var tbcell4 = document.getElementById(tablename).rows[rowIndex].cells[7].innerHTML; //数量
    var tbcell5 = document.getElementById(tablename).rows[rowIndex].cells[4].childNodes[0].value; //最高分
    var tbcell6 = document.getElementById(tablename).rows[rowIndex].cells[5].childNodes[0].value; //完成数量
    var tbcell7 = document.getElementById(tablename).rows[rowIndex].cells[6].childNodes[0].value; //单项总分
    var tbcell0 = document.getElementById(tablename).rows[rowIndex].cells[1].childNodes[0].checked;
    var checkdisabled = document.getElementById(tablename).rows[rowIndex].cells[1].childNodes[0].disabled; //是否默认菜单项
    var hidQuantity = document.getElementById(tablename).rows[rowIndex].cells[1].childNodes[12].value; //完成数量 hid
  
    if (checkdisabled) {
       
        if (parseInt(obj.value) <parseInt( hidQuantity)) {
            obj.value = hidQuantity;
        }
    } else {
        if (!tbcell0) {//如果打勾false则都为0
            document.getElementById(tablename).rows[rowIndex].cells[5].childNodes[0].value = '0'; //完成数量
            document.getElementById(tablename).rows[rowIndex].cells[6].childNodes[0].value = '0'; //单项总分
        } 
    }
    var Agg = 0;

    if (tbcell5 == 0) {

        Agg = parseInt(obj.value) / parseInt(tbcell4) * parseInt(tbcell3);

        document.getElementById(tablename).rows[rowIndex].cells[6].childNodes[0].value = parseInt(Agg);
    }
    else {

        Agg = parseInt(obj.value) / parseInt(tbcell4) * parseInt(tbcell3);

        if (Agg > parseInt(tbcell5 == 'NaN' ? '0' : tbcell5)) {

            document.getElementById(tablename).rows[rowIndex].cells[6].childNodes[0].value = parseInt(tbcell5 == NaN ? '0' : tbcell5)
        }
        else
        { document.getElementById(tablename).rows[rowIndex].cells[6].childNodes[0].value = parseInt(Agg); }
    }
    if (tbcell6 == '') {
        document.getElementById(tablename).rows[rowIndex].cells[5].childNodes[0].value = '0';

    }

    //            else {
    //document.getElementById(tablename).rows[rowIndex].cells[6].childNodes[0].value = (Agg == NaN ? '0' : Agg);

    //}
    if (document.getElementById(tablename).rows[rowIndex].cells[6].childNodes[0].value == 'NaN') {
        document.getElementById(tablename).rows[rowIndex].cells[6].childNodes[0].value = '0'
    }
    var tabnumn = 0;

    for (var i = 1; i < document.getElementById(tablename).rows.length - 2; i++) {
        if (document.getElementById(tablename).rows[i].cells[1].childNodes[0].checked || document.getElementById(tablename).rows[i].cells[1].childNodes[0].disabled) {
            tabnumn += parseInt(document.getElementById(tablename).rows[i].cells[6].childNodes[0].value == '' ? '0' : parseInt(document.getElementById(tablename).rows[i].cells[6].childNodes[0].value));
        }
       
    }

    document.getElementById('txtallnum' + n).value = tabnumn;
    if (parseInt(document.getElementById('txtallnum' + n).value) > parseInt(document.getElementById('txtTotal' + n).value)) {
        //document.getElementById('btnAdd').value = "保存工作设置(设置分数大于最低分数)";
    }
    else {
        //document.getElementById('btnAdd').value = "保存工作设置(设置分数小于最低分数)";
    }

}